﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class CX_XFHZControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        CustomGridView CustomGridList1;
        DropDownList DropDownList1;
        MPCalendar Calendar1, Calendar2;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack) {
                QSDataDLL();
                Bind();
            }
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            Button btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(Go_Search);
        }
        protected void InitControl() {
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            Calendar1 = (MPCalendar)FindControl("Calendar1");
            Calendar2 = (MPCalendar)FindControl("Calendar2");
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            DataTable dt = _EluZSDataProvider.GetXFHZ("0", Calendar1.Text, Calendar2.Text, DropDownList1.SelectedValue,userid);
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataBind();
            
        }

        
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strRS = string.Empty;
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string strQS = e.Row.Cells[3].Text;
                if (!string.IsNullOrEmpty(strQS) && strQS != "&nbsp;")
                {
                    DataTable dt = _EluZSDataProvider.GetXFHZ("1", "", "", strQS,userid);
                    if (dt.Rows.Count > 0)
                    {
                        strRS = dt.Rows[0]["人数"].ToString();
                    }
                    if (string.IsNullOrEmpty(strRS)) { strRS = "0"; }
                    e.Row.Cells[4].Text = strRS;
                    e.Row.Cells[4].Attributes.Add("style", "cursor:pointer;color:blue;");
                    e.Row.Cells[4].Attributes.Add("onclick", "javascript:window.location.href=('../XFManage/XF_SFXX.aspx?type=" + strQS + "');");
                }
            }
        }
        
        protected void QSDataDLL()
        {
            //List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("区属");
            //EluZSProvider.Params item = new EluZSProvider.Params();
            //_params.Insert(0, item);
            DataTable dt = _EluZSDataProvider.GetParamYB("区属", GetSqlWhere());
            DropDownList1.DataSource = dt;
            DropDownList1.DataValueField = "keyname";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "");
        }

        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%' ";
            }
            return sqlWhere;
        }
    }
}


